# Copyright (c) 2013, 2015 Ruslan Baratov
# All rights reserved.

cmake_minimum_required(VERSION 3.5)
project(Hunter)

include(ExternalProject) # ExternalProject_Add

# Scheme for download and install OpenSSL iOS library

list(APPEND CMAKE_MODULE_PATH "@HUNTER_SELF@/cmake/modules")

include(hunter_dump_cmake_flags)
include(hunter_status_debug)
include(hunter_assert_not_empty_string)
include(hunter_unsetvar)

hunter_status_debug("Scheme: url_sha1_openssl_ios")

# Check preconditions
hunter_assert_not_empty_string("@HUNTER_SELF@")
hunter_assert_not_empty_string("@HUNTER_EP_NAME@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_URL@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_SHA1@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_DOWNLOAD_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_SOURCE_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_INSTALL_PREFIX@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_LICENSE_DIR@")
hunter_assert_not_empty_string("@HUNTER_GLOBAL_SCRIPT_DIR@")
hunter_assert_not_empty_string("@HUNTER_TLS_VERIFY@")

# Note: support for single architecture/native iOS builds (i.e., arm64)
hunter_assert_not_empty_string("@IPHONEOS_ARCHS@@IPHONESIMULATOR_ARCHS@")
hunter_assert_not_empty_string("@IOS_SDK_VERSION@")
hunter_assert_not_empty_string("@IPHONESIMULATOR_ROOT@")
hunter_assert_not_empty_string("@IPHONEOS_ROOT@")

hunter_dump_cmake_flags(SKIP_INCLUDES)
# -> CMAKE_CXX_FLAGS
# -> CMAKE_C_FLAGS

string(REGEX REPLACE "^ " "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")

hunter_unsetvar(ssl_input_libraries)
hunter_unsetvar(crypto_input_libraries)

set(configure_opts threads no-shared)
# Pass C flags through
set(configure_opts ${configure_opts} ${CMAKE_C_FLAGS})

foreach(arch @IPHONEOS_ARCHS@ @IPHONESIMULATOR_ARCHS@)
  list(
      APPEND
      ssl_input_libraries
      "@HUNTER_PACKAGE_SOURCE_DIR@/${arch}/libssl.a"
   )
  list(
      APPEND
      crypto_input_libraries
      "@HUNTER_PACKAGE_SOURCE_DIR@/${arch}/libcrypto.a"
   )
endforeach()

# fuse all libraries by 'lipo'
ExternalProject_Add(
    "@HUNTER_EP_NAME@-ios_universal"
    DOWNLOAD_COMMAND
    ""
    DOWNLOAD_DIR
    "@HUNTER_PACKAGE_DOWNLOAD_DIR@"
    TLS_VERIFY
    "@HUNTER_TLS_VERIFY@"
    SOURCE_DIR
    "@HUNTER_PACKAGE_SOURCE_DIR@/ios_universal"
    INSTALL_DIR
    "@HUNTER_PACKAGE_INSTALL_PREFIX@"
        # not used, just avoid creating Install/<name> empty directory
    CONFIGURE_COMMAND
    ""
    BUILD_COMMAND
    ""
    INSTALL_COMMAND
    lipo
    -create
    ${ssl_input_libraries}
    -o
    "@HUNTER_PACKAGE_INSTALL_PREFIX@/lib/libssl.a"
    COMMAND
    lipo
    -create
    ${crypto_input_libraries}
    -o
    "@HUNTER_PACKAGE_INSTALL_PREFIX@/lib/libcrypto.a"
)

foreach(variant @IPHONEOS_ARCHS@ @IPHONESIMULATOR_ARCHS@)
  set(iphoneos_archs @IPHONEOS_ARCHS@)
  list(FIND iphoneos_archs ${variant} find_index)
  if(find_index EQUAL -1)
    set(CROSS_TOP "@IPHONESIMULATOR_ROOT@")
    set(CROSS_SDK "iPhoneSimulator@IOS_SDK_VERSION@.sdk")
    set(SDK_ROOT ${IPHONESIMULATOR_SDK_ROOT})
  else()
    set(CROSS_TOP "@IPHONEOS_ROOT@")
    set(CROSS_SDK "iPhoneOS@IOS_SDK_VERSION@.sdk")
    set(SDK_ROOT ${IPHONEOS_SDK_ROOT})
  endif()

  string(COMPARE EQUAL "${variant}" "arm" arm)
  string(COMPARE EQUAL "${variant}" "armv7" armv7)
  if(arm OR armv7)
    set(target "ios-cross")
  endif()

  string(COMPARE EQUAL "${variant}" "arm64" ios64_cross)
  if(ios64_cross)
    set(target "ios64-cross")
  endif()

  string(COMPARE EQUAL "${variant}" "i386" i386)
  if(i386)
    set(target "darwin-i386-cc")
  endif()

  string(COMPARE EQUAL "${variant}" "x86_64" x86_64)
  if(x86_64)
    set(target "darwin64-x86_64-cc")
    # set_key.c:398:17: error: unsupported inline asm:
    # input with type 'unsigned long' matching output with type 'unsigned int'
    set(configure_opts ${configure_opts} "no-asm")
  endif()

  ExternalProject_Add(
      "@HUNTER_EP_NAME@-${variant}"
      URL
      @HUNTER_PACKAGE_URL@
      URL_HASH
      SHA1=@HUNTER_PACKAGE_SHA1@
      DOWNLOAD_DIR
      "@HUNTER_PACKAGE_DOWNLOAD_DIR@"
      TLS_VERIFY
      "@HUNTER_TLS_VERIFY@"
      SOURCE_DIR
      "@HUNTER_PACKAGE_SOURCE_DIR@/${variant}"
      INSTALL_DIR
      "@HUNTER_PACKAGE_INSTALL_PREFIX@"
          # not used, just avoid creating Install/<name> empty directory
      CONFIGURE_COMMAND
      ./Configure
      "${target}"
      "${configure_opts}"
      "no-async"
      "--prefix=@HUNTER_PACKAGE_INSTALL_PREFIX@"
      "--libdir=lib"
      "-arch ${variant}"
      "-isysroot ${SDK_ROOT}"
      BUILD_COMMAND
      . "@HUNTER_GLOBAL_SCRIPT_DIR@/clear-all.sh" &&
      export CROSS_TOP=${CROSS_TOP} &&
      export CROSS_SDK=${CROSS_SDK} &&
      make
      BUILD_IN_SOURCE
      1
      INSTALL_COMMAND
      make install_sw
          # installed libraries will be rewrited,
          # other files are equal
      COMMAND # Copy license files
      "@CMAKE_COMMAND@"
      "-C@HUNTER_ARGS_FILE@" # for 'HUNTER_INSTALL_LICENSE_FILES'
      "-Dsrcdir=@HUNTER_PACKAGE_SOURCE_DIR@/${variant}"
      "-Ddstdir=@HUNTER_PACKAGE_LICENSE_DIR@"
      -P
      "@HUNTER_SELF@/scripts/try-copy-license.cmake"
  )

  add_dependencies(
      "@HUNTER_EP_NAME@-ios_universal"
      "@HUNTER_EP_NAME@-${variant}"
  )
endforeach()
